package logic

import (
	"fmt"
	"github.com/go-resty/resty/v2"
	"imagesearch/internal/config"
)

type Feature struct {
	Feature string `json:"feature""`
}

type VearchData struct {
	ItemName string  `json:"name"`
	IMG_URL  string  `json:"url"`
	Feature1 Feature `json:"feature1"`
}

type VearchResponse struct {
	Index  string `json:"_index"`
	Type   string `json:"_type"`
	ID     string `json:"_id"`
	Status int    `json:"status"`
}

func InsertToVearch(config config.VearchConfig, vearchData VearchData) int {

	var requestUrl = config.Url + "/" + config.DbName + "/" + config.SpaceName + "/"
	client := resty.New()
	result := &VearchResponse{}

	res, err := client.R().SetResult(result).SetHeader("Content-type", "application/json").
		ForceContentType("application/json").
		SetBody(vearchData).
		Post(requestUrl)
	fmt.Println("InsertToVearch", requestUrl, vearchData.Feature1, res.String(), err)

	if err != nil {
		fmt.Println("InsertToVearch error:", err)
		return -1
	}
	if res.StatusCode() != 200 {
		fmt.Println("InsertToVearch server error:", res.String())
		return -1
	}

	return 0
}
